#!/user/bin/env python3
# -*- coding: utf-8 -*-
import requests,time, cgi, cgitb

url = 'https://www.icve.com.cn/study/Directory/directoryList?courseId=wtnaabiqtzngdrqjadkyma'
ari_url = 'https://www.icve.com.cn/study/directory/view'

while True:
    User_Cookie = input('请输入登录的Cookie：')
    headers1 = {
        'Cookie': User_Cookie,
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
        'referer': 'https://www.icve.com.cn/study/directory/directory_list.html?courseId=wtnaabiqtzngdrqjadkyma'
    }

    headers2 = {
        'Cookie': User_Cookie,
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
           'Referer': 'Referer: https://www.icve.com.cn/study/directory/dir_course.html?courseId=wtnaabiqtzngdrqjadkyma&chapterId=bdnaabiqa7tkhapv0oujuw&sort=6.002',

    }

    try:
        print('正在获取音频文本目录中 。。。 ')
        rp = requests.get(url, headers=headers1).json()['directory']
        print('获取成功。。。')

        for directory_items in rp:
            '''遍历单元 （4个）（单元元素是列表类型） '''
            subtitle_items = directory_items['chapters']  # 将单元中的章节赋值给subtitle_items
            for chapters_items in subtitle_items:
                '''遍历每个章节'''
                st_chapters_item = chapters_items['cells']
                rd_chapters_item = chapters_items['knowleges']
                for nd_video_text in st_chapters_item:
                    '''遍历二级元素'''
                    text = nd_video_text['CellType']  # 文件类型
                    ChapterId_one = nd_video_text['ChapterId']  # 后缀1
                    ChapterId_two = nd_video_text['Id']  # 后缀2
                    Chapter_title = nd_video_text['Title']  # 二级标题
                    Chapter_SortOrder = nd_video_text['SortOrder']  # 二级排序

                    print('二级标题' + Chapter_title, '二级后缀1' + ChapterId_one, '二级后缀2：' + ChapterId_two, '二级文件类型：' + text)
                    print('正在访问二级标题')
                    data1 = {
                        'cellId': ChapterId_two,
                        'courseId': 'wtnaabiqtzngdrqjadkyma',
                        'enterType': 'study'
                    }
                    if text == 'video':

                        response_time1 = requests.post(ari_url, data=data1, headers=headers2).json()  # 学习时长url

                        learn_time = response_time1['data']['learnTime']  # 学习时间
                        print(type(learn_time))
                        data_status1 = {
                            'cellId': ChapterId_two,
                            'learntime': learn_time,
                            'status': 1
                        }
                        ariver_status = 'https://www.icve.com.cn/study/directory/updateStatus'

                        rp = requests.post(ariver_status, data=data_status1, headers=headers2)
                        print('访问' + '二级标题' + Chapter_title + '成功')

                    elif text == 'audio' or text == 'text' or text == 'ppt':
                        audio_text_data = {
                            'cellId': ChapterId_two,
                            'courseId': 'wtnaabiqtzngdrqjadkyma',
                            'enterType': 'study'
                        }

                        audio_text_rq = requests.post(ari_url, data=audio_text_data, headers=headers1)
                        print('访问二级文本,录音,ppt成功！')

                        print('访问' + '二级标题' + Chapter_title + '成功')
                        time.sleep(8)

                for rd_video_text in rd_chapters_item:
                    '''遍历三级元素'''
                    rd_video_text_list = rd_video_text['cells']
                    for rd_video_text_item in rd_video_text_list:
                        rd_file_type = rd_video_text_item['CellType']  # 三级文件类型
                        rd_chapters_Id = rd_video_text_item['ChapterId']  # 三级后缀1
                        rd_video_id = rd_video_text_item['Id']  # 三级后缀2
                        rd_title = rd_video_text_item['Title']  # 三级标题
                        rd_SortOrder = rd_video_text_item['SortOrder']  # 三级排序

                        # print('三级标题：' + rd_title,'三级后缀1：' + rd_chapters_Id,'三级后缀2：' + rd_video_id,'三级文件类型: '+ rd_file_type)
                        print('正在访问三级课程。。。。')
                        data2 = {
                            'cellId': rd_video_id,
                            'courseId': 'wtnaabiqtzngdrqjadkyma',
                            'enterType': 'study'
                        }

                        if rd_file_type == 'video':
                            response_time2 = requests.post(ari_url, data=data2, headers=headers2).json()  # 学习时长url
                            learn_time = response_time2['data']['learnTime']  # 学习时间
                            ari_data = {
                                'cellId': rd_video_id,
                                'learntime': learn_time,
                                'status': 1
                            }

                            ariver_status = 'https://www.icve.com.cn/study/directory/updateStatus'

                            rp = requests.post(ariver_status, data=ari_data, headers=headers2)
                            print('访问' + '三级标题' + rd_title + '成功')

                        elif rd_file_type == 'audio' or rd_file_type == 'text' or rd_file_type == 'ppt':
                            audio_text_data1 = {
                                'cellId': rd_video_id,
                                'courseId': 'wtnaabiqtzngdrqjadkyma',
                                'enterType': 'study'
                            }
                            rd_audio_text_req = requests.post(ari_url, data=audio_text_data1, headers=headers1)
                            print('访问三级文本,录音或者ppt成功！')
                        print('访问' + '三级标题' + rd_title + '成功')
                    time.sleep(9)

        print('刷课成功')
        ask_then = input('是否继续"y" or "n" ')
        if ask_then == 'y':
            continue
        elif ask_then == 'n':
            break
        else:
            print('请输入正确的字符')

    except Exception as e:
        print(e)